﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace fileviewer
{
    public partial class Form1 : Form
    {
        private string rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeTreeView()
        {
            treeView1.Nodes.Clear();
            var root = new TreeNode(rootDirectory);
            treeView1.Nodes.Add(root);
            PopulateTreeView(root);
            root.Expand();
        }
        private void PopulateTreeView(TreeNode node)
        {
            try
            {
                var directories = Directory.GetDirectories(node.FullPath);
                foreach (var directory in directories)
                {
                    var child = new TreeNode(Path.GetFileName(directory));
                    node.Nodes.Add(child);
                    PopulateTreeView(child);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void PopulateListView(string directory)
        {
            listView1.Items.Clear();
            try
            {
                var directories = Directory.GetDirectories(directory);
                foreach (var directoryPath in directories)
                {
                    var info = new DirectoryInfo(directoryPath);
                    var item = new ListViewItem(info.Name);
                    item.SubItems.Add("File folder");
                    item.SubItems.Add("");
                    item.SubItems.Add(info.LastWriteTime.ToShortDateString());
                    item.SubItems.Add(directoryPath);
                    listView1.Items.Add(item);
                }

                var files = Directory.GetFiles(directory, "*.txt");
                foreach (var filePath in files)
                {
                    var info = new FileInfo(filePath);
                    var item = new ListViewItem(info.Name);
                    item.SubItems.Add("Text Document");
                    item.SubItems.Add(info.Length.ToString("N0"));
                    item.SubItems.Add(info.LastWriteTime.ToShortDateString());
                    item.SubItems.Add(filePath);
                    listView1.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var directory = e.Node.FullPath;
            PopulateListView(directory);
        }

        private void open_Click(object sender, EventArgs e)
        {
            InitializeTreeView();
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var selected = listView1.SelectedItems;
            if (selected.Count == 1)
            {
                var path = selected[0].SubItems[4].Text;
                var extension = Path.GetExtension(path);
                if (extension == ".txt")
                {
                    try
                    {
                        System.Diagnostics.Process.Start(path);
                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }
}
